function h_control = guipanel(h_fig, panel_pos, orient_flag, varargin);

% GUIPANEL - create a control panel with the provided parameters
%
%    H_CTL = GUIPANEL(H_FIG, POS, 'horizontal', 'Name1', value1, ...)
%    creates a horizontal panel of controls on the figure window H_FIG.
%    The location of the panel is determined by the values in POS, a
%    four element vector containing the normalized coordinate values
%    [LEFT BOTTOM WIDTH HEIGHT].  The control properties are specified
%    by pairs of the form NAME/VALUE, where the property name is
%    provided by a string NAME and the property values for each control
%    are given by a subsequent cell array VALUE.  The control object
%    handles are returned as a vector H_CTL.
%
%    H_CTL = GUIPANEL(H_FIG, POS, 'vertical', ...) creates a vertical
%    panel of controls.
%
%    H_CTL = GUIPANEL(H_FIG, POS, [NR NC], ...) creates a panel of
%    controls having NR rows and NC columns of controls.

% By:   S.C. Molitor (smolitor@bme.jhu.edu)
% Date: April 22, 1999

% check input parameters

if (nargin < 5)
   return
elseif (~istype(h_fig, 'figure'))
   return
elseif (~isnumeric(panel_pos))
   return
elseif (length(panel_pos) ~= 4)
   return
end

% make sure inputs are property name/value pairs
% create cell array to store name/value pairs
% count number of controls to be created

num_control = 0;
num_prop = 0;
for i = 1 : 2 : length(varargin)
   if (ischar(varargin{i}) & iscell(varargin{i + 1}))
      num_prop = num_prop + 1;
      prop_name{num_prop} = varargin{i};
      var_value = varargin{i + 1};
      if (num_control == 0)
         num_control = length(var_value);
      else
         num_control = min(num_control, length(var_value));
      end
      for j = 1 : num_control
         prop_value{j, num_prop} = var_value{j};
      end
   else
      return
   end
end

% calculate layout of controls
% default layout is vertical

if (ischar(orient_flag))
   if (strcmp(orient_flag, 'horizontal'))
      num_rows = 1;
      num_cols = num_control;
   else
      num_rows = num_control;
      num_cols = 1;
   end
elseif (isnumeric(orient_flag))
   if (length(orient_flag) == 2)
      num_rows = orient_flag(1);
      num_cols = orient_flag(2);
   else
      num_rows = num_control;
      num_cols = 1;
   end
else
   num_rows = num_control;
   num_cols = 1;
end

% layout controls

control_width = panel_pos(3)/num_cols;
control_height = panel_pos(4)/num_rows;
control_bottom = panel_pos(2) + panel_pos(4);
control_count = 0;
for i = 1 : num_rows
   control_left = panel_pos(1);
   control_bottom = control_bottom - control_height;
   for j = 1 : num_cols
      control_count = control_count + 1;
      h_control(control_count) = uicontrol(...
         'Parent', h_fig, ...
         'Units', 'normalized', ...
         'Position', [control_left control_bottom control_width control_height]);
      if (control_count <= num_control)
         for k = 1 : num_prop
            set(h_control(control_count), prop_name{k}, prop_value{control_count, k});
         end
      end
      control_left = control_left + control_width;
   end
end
return
